package cn.liangxxhu.gateway.controller;

import cn.liangxxhu.gateway.mq.domain.PayResultCallbackOrderEvent;
import cn.liangxxhu.gateway.mq.producer.OrderCancelSendProduce;
import cn.liangxxhu.rocketmq.enums.MessageTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Tag(name = "测试接口", description = "用于演示 RocketMQ 消息发送等测试能力")
public class TestController {

    private final OrderCancelSendProduce orderCancelSendProduce;

    @GetMapping("/send")
    @Operation(summary = "发送延迟关闭订单消息")
    public void senMessage() {
        PayResultCallbackOrderEvent orderEvent = PayResultCallbackOrderEvent.builder()
                .orderSn(UUID.randomUUID().toString())
                .build();
        SendResult sendResult = orderCancelSendProduce.send(orderEvent, MessageTypeEnum.DELAY.getCode());
        if (!Objects.equals(sendResult.getSendStatus(), SendStatus.SEND_OK)) {
            throw new RuntimeException("投递延迟关闭订单消息队列失败");
        }
    }

}
